package com.long1008.service;

import com.long1008.pojo.*;
import com.long1008.mapper.PersonnelMapper;
import com.long1008.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PersonnelService {

    @Autowired
    PersonnelMapper personnelMapper;

    @Resource
    EmpService empService;

    public int addEc(EmpEc empEc) {
        return personnelMapper.addEc(empEc);
    }

    public int addRetire(EmpRetire empRetire) {
        return personnelMapper.addRetire(empRetire);
    }

    public List<EmpEc> getAllEmpEc() {
        return personnelMapper.getAllEmpEc();
    }

    public List<EmpRetire> getAllEmpRetires() {
        return personnelMapper.getAllEmpRetires();
    }

    public int addEmpTrain(EmpTrain empTrain) {
        return personnelMapper.addEmpTrain(empTrain);
    }

    @Transactional
    public RespPageEntity getTrainsByPage(Integer page, Integer size) {
        RespPageEntity pageEntity = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<EmpTrain> empTrains = personnelMapper.getTrainsByPage(page, size);
        for (EmpTrain empTrain : empTrains) {
            Employee employee = empService.getEmpById(empTrain.getEid());
            empTrain.setWorkID(String.format("%08d", empTrain.getEid()));
            empTrain.setEmpName(employee.getName());
        }
        // 使用匿名比较器排序
        empTrains.sort((s1, s2) -> {
            if (s1.getTrainDate().before(s2.getTrainDate())) {
                return -1;
            } else if (s1.getTrainDate().after(s2.getTrainDate())) {
                return 1;
            } else {
                return 0;
            }
        });
        pageEntity.setData(empTrains);
        // 获取当前用户总量
        Long totalCount = personnelMapper.getTrainCount();
        pageEntity.setTotal(totalCount);
        return pageEntity;
    }

    @Transactional
    public RespPageEntity getAdjustSalarysByPage(Integer page, Integer size) {
        RespPageEntity pageEntity = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<AdjustSalary> adjustSalarys = personnelMapper.getAdjustSalarysByPage(page, size);
        for (AdjustSalary adjustSalary : adjustSalarys) {
            Employee employee = empService.getEmpById(adjustSalary.getEid());
            adjustSalary.setWorkID(String.format("%08d", adjustSalary.getEid()));
            adjustSalary.setEmpName(employee.getName());
        }
        // 使用匿名比较器排序
        adjustSalarys.sort((s1, s2) -> {
            if (s1.getAsDate().before(s2.getAsDate())) {
                return -1;
            } else if (s1.getAsDate().after(s2.getAsDate())) {
                return 1;
            } else {
                return 0;
            }
        });
        pageEntity.setData(adjustSalarys);
        // 获取当前用户总量
        Long totalCount = personnelMapper.getAdjustSalaryCount();
        pageEntity.setTotal(totalCount);
        return pageEntity;
    }

    @Transactional
    public RespPageEntity getTrainsByEmpName(String empName, Integer page, Integer size) {
        RespPageEntity pageEntity = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<EmpTrain> empTrains = personnelMapper.getTrainsByEmpName(empName, page, size);
        for (EmpTrain empTrain : empTrains) {
            Employee employee = empService.getEmpById(empTrain.getEid());
            empTrain.setWorkID(String.format("%08d", empTrain.getEid()));
            empTrain.setEmpName(employee.getName());
        }
        pageEntity.setData(empTrains);
        // 获取当前用户总量
        Long totalCount = personnelMapper.getTotalByEmpName(empName);
        pageEntity.setTotal(totalCount);
        return pageEntity;
    }

    @Transactional
    public RespPageEntity getAdjustSalarysByKeywords(String keywords, Integer page, Integer size) {
        RespPageEntity pageEntity = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<AdjustSalary> adjustSalarys = personnelMapper.getAdjustSalarysByKeywords(keywords, page, size);
        for (AdjustSalary adjustSalary : adjustSalarys) {
            Employee employee = empService.getEmpById(adjustSalary.getEid());
            adjustSalary.setWorkID(String.format("%08d", adjustSalary.getEid()));
            adjustSalary.setEmpName(employee.getName());
        }
        pageEntity.setData(adjustSalarys);
        // 获取当前用户总量
        Long totalCount = personnelMapper.getAdjustSalaryTotalCountByKewords(keywords);
        pageEntity.setTotal(totalCount);
        return pageEntity;
    }

    @Transactional
    public RespPageEntity getAdjustSalarysByDate(Date date, Integer page, Integer size) {
        RespPageEntity pageEntity = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<AdjustSalary> adjustSalarys = personnelMapper.getAdjustSalarysByDate(date, page, size);
        for (AdjustSalary adjustSalary : adjustSalarys) {
            Employee employee = empService.getEmpById(adjustSalary.getEid());
            adjustSalary.setWorkID(String.format("%08d", adjustSalary.getEid()));
            adjustSalary.setEmpName(employee.getName());
        }
        pageEntity.setData(adjustSalarys);
        // 获取当前用户总量
        Long totalCount = personnelMapper.getAdjustSalaryTotalCountByDate(date);
        pageEntity.setTotal(totalCount);
        return pageEntity;
    }

    @Transactional
    public RespPageEntity getTrainsByTrainDate(Date trainDate, Integer page, Integer size) {
        RespPageEntity pageEntity = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<EmpTrain> empTrains = personnelMapper.getTrainsByTrainDate(trainDate, page, size);
        for (EmpTrain empTrain : empTrains) {
            Employee employee = empService.getEmpById(empTrain.getEid());
            empTrain.setWorkID(String.format("%08d", empTrain.getEid()));
            empTrain.setEmpName(employee.getName());
        }
        pageEntity.setData(empTrains);
        // 获取当前用户总量
        Long totalCount = personnelMapper.getTotalByTrainDate(trainDate);
        pageEntity.setTotal(totalCount);
        return pageEntity;
    }

    public Long getTrainCount() {
        return personnelMapper.getTrainCount();
    }

    public int updateEmpTrain(EmpTrain empTrain) {
        return personnelMapper.updateEmpTrain(empTrain);
    }

    public int deleteEmpTrain(String ids) {
        String[] split = ids.split(",");
        return personnelMapper.deleteEmpTrain(split);
    }

    public List<EmpTrain> getAllEmpTrains() {
        return personnelMapper.getAllEmpTrains();
    }

    public int addAdjustSalary(AdjustSalary adjustSalary) {
        return personnelMapper.addAdjustSalary(adjustSalary);
    }

    //更新实际薪资（调薪）
    public int updateAdjustSalaryToAllSalary(AllSalary allSalary) {
        return personnelMapper.updateAdjustSalaryToAllSalary(allSalary);
    }

    // 添加账套薪资
    public int addAdjustSalaryToAllSalary(AllSalary allSalary) {
        return personnelMapper.addAdjustSalaryToAllSalary(allSalary);
    }

    public int updateAdjustSalary(AdjustSalary adjustSalary) {
        return personnelMapper.updateAdjustSalary(adjustSalary);
    }

    public int deleteAdjustSalary(String ids) {
        String[] split = ids.split(",");
        return personnelMapper.deleteAdjustSalary(split);
    }

    public List<AdjustSalary> getAllAdjustSalary() {
        return personnelMapper.getAllAdjustSalary();
    }


    public int addEmpMove(EmpMove empMove) {
        return personnelMapper.addEmpMove(empMove);
    }

    public int updateEmpMove(EmpMove empMove) {
        return personnelMapper.updateEmpMove(empMove);
    }

    public int deleteEmpMove(String ids) {
        String[] split = ids.split(",");
        return personnelMapper.deleteEmpMove(split);
    }

    public List<EmpMove> getAllEmpMove() {
        return personnelMapper.getAllEmpMove();
    }

    public int updateDepIdAndJobId(EmpMove empMove) {
        return personnelMapper.updateDepIdAndJobId(empMove);
    }

    @Transactional
    public RespPageEntity getMovesByPage(Integer page, Integer size) {
        RespPageEntity pageEntity = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<EmpMove> empMoves = personnelMapper.getMovesByPage(page, size);
        for (EmpMove empMove : empMoves) {
            Employee employee = empService.getEmpById(empMove.getEid());
            empMove.setWorkID(String.format("%08d", empMove.getEid()));
            empMove.setEmpName(employee.getName());
        }
        // 使用匿名比较器排序
        empMoves.sort((s1, s2) -> {
            if (s1.getRemoveDate().before(s2.getRemoveDate())) {
                return -1;
            } else if (s1.getRemoveDate().after(s2.getRemoveDate())) {
                return 1;
            } else {
                return 0;
            }
        });
        pageEntity.setData(empMoves);
        // 获取当前用户总量
        Long totalCount = personnelMapper.getMovesCountByPage();
        pageEntity.setTotal(totalCount);
        return pageEntity;
    }

    public RespPageEntity getMovesByKeywords(String keywords, Integer page, Integer size) {
        RespPageEntity pageEntity = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        // 获取当前也用户信息
        List<EmpMove> empMoves = personnelMapper.getMoveByKeywords(keywords);
        for (EmpMove empMove : empMoves) {
            Employee employee = empService.getEmpById(empMove.getEid());
            empMove.setWorkID(String.format("%08d", empMove.getEid()));
            empMove.setEmpName(employee.getName());
        }
        pageEntity.setData(empMoves);
        // 获取当前用户总量
        Long totalCount = personnelMapper.getTotalByKeywords(keywords);
        pageEntity.setTotal(totalCount);
        return pageEntity;
    }

}
